<?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 * 925Routes Routes Model
 *
 * @package      925CMS
 * @subpackage  925Routes
 * @category      Models
 * @author      HoangNQ
 */
class Routes_m extends MY_Model
{

    /* Fields */
    public $fields = array(
        array('field' => 'route_key', 'label' => 'Route key', 'rules' => 'max_length[200]'),
        array('field' => 'route_value', 'label' => 'Route value', 'rules' => 'required|max_length[200]')
    );

    // --------------------------------------------------------------------------

    /**
     * Get routes
     *
     * @access    public
     * @param    bool $limit
     * @param    bool $offset
     * @return obj
     */
    public function get_routes($limit = FALSE, $offset = FALSE)
    {
        $this->db->order_by('route_key', 'asc');

        //$this->db->limit($limit);

        $obj = $this->db->get('routes');

        return $obj->result();
    }

    // --------------------------------------------------------------------------

    /**
     * Get a single route by ID
     *
     * @access    public
     * @param    int $route_id
     * @return    obj
     */
    public function get_route($route_id)
    {
        $obj = $this->db->limit(1)->where('id', $route_id)->get('routes');

        if ($obj->num_rows() == 0) return null;

        return $obj->row();
    }

    // --------------------------------------------------------------------------

    /**
     * Add a route into the db
     *
     * @access    public
     * @return     bool
     */
    public function add_route()
    {
        $insert_data = array(
            'route_key' => $this->input->post('route_key'),
            'route_value' => $this->input->post('route_value'),
            'when_added' => date('Y-m-d H:i:s'),
            'added_by' => $this->current_user->id
        );

        return $this->db->insert('routes', $insert_data);
    }

    // --------------------------------------------------------------------------

    /**
     * Update a route in the db
     *
     * @access    public
     * @param    int route_id
     * @return     bool
     */
    public function update_route($route_id)
    {
        $update_data = array(
            'route_key' => $this->input->post('route_key'),
            'route_value' => $this->input->post('route_value'),
            'last_updated' => date('Y-m-d H:i:s')
        );

        $this->db->where('id', $route_id);
        return $this->db->update('routes', $update_data);
    }

    // --------------------------------------------------------------------------

    /**
     * Update a route in the db
     *
     * @access    public
     * @param    int route_id
     * @return     bool
     */
    public function delete_route($route_id)
    {
        $this->db->where('id', $route_id);
        return $this->db->delete('routes');
    }

    // --------------------------------------------------------------------------

    /**
     * Sync routes
     *
     * @access    public
     * @return     bool
     */
    public function sync_routes()
    {
        $this->load->helper('file');

        // Check to make sure that we can read/write the
        // Routes file
        $route_file = APPPATH . 'config/routes.php';

        $info = get_file_info($route_file, 'writable');

        // Does it even exist? Is it writable?
        if (!$info or !$info['writable']) return false;

        $path = APPPATH . 'modules/routes/';

        if (!is_file($default_route_file = $path . 'default_routes.php')) ;

        // Let's start our routes file!
        $file_data = read_file($default_route_file) . "\n";

        // Get the routes
        $routes = $this->get_routes();

        if ($routes):

            $file_data .= "\n/* Custom Routes from Routes Modules */\n\n";

            foreach ($routes as $route):

                $file_data .= "\$route['{$route->route_key}'] = '{$route->route_value}';\n";

            endforeach;

        endif;

        $file_data .= "\n" . '/* End of file routes.php */';

        // Clear the file first
        file_put_contents($route_file, '');

        // Write the file
        return write_file($route_file, $file_data, 'r+');
    }
}

/* End of file routes_m.php */
/* Location: ./addons/modules/routes/models/routes_m.php */